package com.oauth.util;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.oauth.model.AuthUrl;
import com.oauth.model.Website;

import android.content.Context;
import android.util.Log;

public class ProfileIO {
	public static Website ReadXML(Context context, String fileName) {
		DocumentBuilderFactory docBuilderFactory = null;
		DocumentBuilder docBuilder = null;
		Document doc = null;
		Website ws = new Website();
		try {
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docBuilderFactory.newDocumentBuilder();
			// xml file 放到 assets目录中的
			doc = docBuilder.parse(context.getResources().getAssets()
					.open(fileName));
			// root element
			Element root = doc.getDocumentElement();
			// Do something here
			// get a NodeList by tagname
			NodeList nodeList = root.getElementsByTagName("WebSite");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node nd = nodeList.item(i);
				// 读取站点信息
				ws.name = getNodeParamByName("name", nd);
				ws.version = getNodeParamByName("version", nd);
				ws.app_key = getNodeParamByName("app_key", nd);
				ws.app_secret = getNodeParamByName("app_secret", nd);
				ws.redirect_uri = getNodeParamByName("redirect_uri", nd);
//				if (ws.version.equals("2.0")
//						|| ws.name.toLowerCase().startsWith("xauth")) {
//					readOath2(nd, ws);
//				} else {
//					readOath1(nd, ws);
//				}
				readOath(nd,ws);
			}
		} catch (IOException e) {
		} catch (SAXException e) {
		} catch (ParserConfigurationException e) {
		} finally {
			doc = null;
			docBuilder = null;
			docBuilderFactory = null;
		}
		return ws;
	}

	protected static void readOath(Node nd, Website ws) {

		Node RequestToken = ((Element) nd).getElementsByTagName("RequestToken")
				.item(0);
		Node Authorization = ((Element) nd).getElementsByTagName(
				"Authorization").item(0);

		Node AccessToken = ((Element) nd).getElementsByTagName("AccessToken")
				.item(0);

		if (RequestToken != null) {
			ws.requestToken = new AuthUrl();
			readAuthUrl(RequestToken, ws.requestToken);

		}
		if (Authorization != null) {
			ws.auth = new AuthUrl();
			readAuthUrl(Authorization, ws.auth);
		}
		if (AccessToken != null) {
			ws.accessToken = new AuthUrl();
			readAuthUrl(AccessToken, ws.accessToken);
		}

		Log.i("DEBUG", "XML Loaded");
	}

//	protected static void readOath1(Node nd, Website ws) {
//		ws.requestToken = new AuthUrl();
//		ws.auth = new AuthUrl();
//		ws.accessToken = new AuthUrl();
//		Node RequestToken = ((Element) nd).getElementsByTagName("RequestToken")
//				.item(0);
//		Node Authorization = ((Element) nd).getElementsByTagName(
//				"Authorization").item(0);
//
//		Node AccessToken = ((Element) nd).getElementsByTagName("AccessToken")
//				.item(0);
//		readAuthUrl(RequestToken, ws.requestToken);
//		readAuthUrl(Authorization, ws.auth);
//		readAuthUrl(AccessToken, ws.accessToken);
//		Log.i("DEBUG", "Finish");
//	}
//
//	protected static void readOath2(Node nd, Website ws) {
//		ws.auth = new AuthUrl();
//		ws.accessToken = new AuthUrl();
//		Node Authorization = ((Element) nd).getElementsByTagName(
//				"Authorization").item(0);
//
//		Node AccessToken = ((Element) nd).getElementsByTagName("AccessToken")
//				.item(0);
//
//		readAuthUrl(Authorization, ws.auth);
//		readAuthUrl(AccessToken, ws.accessToken);
//		Log.i("DEBUG", "Finish");
//	}

	/**
	 * 读取参数
	 * @param nd
	 * @param auth
	 */
	protected static void readAuthUrl(Node nd, AuthUrl auth) {
		auth.url = getNodeParamByName("Url", nd);
		auth.method_type = Integer.parseInt(getNodeParamByName("Method", nd));
		Node params = ((Element) nd).getElementsByTagName("Params").item(0);
		NodeList paramList = ((Element) params).getElementsByTagName("Param");
		for (int i = 0; i < paramList.getLength(); i++) {
			Node param = paramList.item(i);
			auth.addParams(getNodeParamByName("name", param),
					getNodeParamByName("value", param));
		}
	}

	
	protected static String getNodeParamByName(String name, Node nd) {
		NamedNodeMap attr = nd.getAttributes();
		Node param = attr.getNamedItem(name);
		return param.getNodeValue();
	}
}
